package com.hqd.sellrelation.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hqd.common.model.PageParams;
import com.hqd.common.mybatis.base.service.impl.BaseServiceImpl;
import com.hqd.sellrelation.server.dto.ResourcePoolDto;
import com.hqd.sellrelation.server.entity.CommunalData;
import com.hqd.sellrelation.server.entity.ResourcePool;
import com.hqd.sellrelation.server.mapper.CommunalDataMapper;
import com.hqd.sellrelation.server.mapper.ResourcePoolMapper;
import com.hqd.sellrelation.server.service.ResourcePoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 资源池实现类
 * wx
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class ResourcePoolServiceImpl extends BaseServiceImpl<ResourcePoolMapper, ResourcePool> implements ResourcePoolService{

    @Autowired
    private ResourcePoolMapper resourcePoolMapper;


    /**
     * 查询资源池
     * wx
     */
    @Override
    public IPage<ResourcePool> findListPage(PageParams pageParams) {
        ResourcePoolDto query = pageParams.mapToObject(ResourcePoolDto.class);
        QueryWrapper<ResourcePool> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(ResourcePool::getIsDel,0)
                .and(ObjectUtils.isNotEmpty(query.getCompanyStatus()),i->i.like(ResourcePool::getCompanyName,query.getKeyWord())
                        .or().eq(ResourcePool::getTelephone,query.getKeyWord()).or().eq(ResourcePool::getPersonName,query.getKeyWord()))
                .eq(ObjectUtils.isNotEmpty(query.getPersonLiableId()),ResourcePool::getPersonLiableId,query.getPersonLiableId())
                .eq(ObjectUtils.isNotEmpty(query.getCompanyStatus()),ResourcePool::getCompanyStatus,query.getCompanyStatus())
                .eq(ObjectUtils.isNotEmpty(query.getCustomerLevel()),ResourcePool::getCustomerLevel,query.getCustomerLevel())
                .eq(ObjectUtils.isNotEmpty(query.getBusinessDepartment()), ResourcePool::getBusinessDepartment,query.getBusinessDepartment())
                .between(ObjectUtils.isNotEmpty(query.getStartCreateTime()),ResourcePool::getCreateTime,query.getStartCreateTime(),query.getEndCreateTime());
        switch (query.getOrder()){
            case "CA":
                queryWrapper.lambda().orderByAsc(ResourcePool::getCreateTime);
                break;
            case "CD":
                queryWrapper.lambda().orderByDesc(ResourcePool::getCreateTime);
                break;
            case "FA":
                queryWrapper.lambda().orderByAsc(ResourcePool::getFollowUpTime);
                break;
            case "FD":
                queryWrapper.lambda().orderByDesc(ResourcePool::getFollowUpTime);
                break;
        }
        return  resourcePoolMapper.selectPage(pageParams,queryWrapper);
    }

    /**
     * 删除资源池信息
     * wx
     */
    @Override
    public void deleteResource(ArrayList<String> resourceId) {
        ResourcePool resourcePool = new ResourcePool();
        resourcePool.setIsDel(1);
        UpdateWrapper<ResourcePool> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .in(ResourcePool::getResourceId,resourceId);
        resourcePoolMapper.update(resourcePool,updateWrapper);
    }
    /**
     * 修改资源池信息
     * wx
     */
    @Override
    public void updateResource(List<ResourcePool> resourcePools){
        for (ResourcePool resourcePool:resourcePools){
        resourcePoolMapper.updateById(resourcePool);
        }
    }
}
